home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995…tember: Reference Library / Dev.CD Sep 95 RL / Dev.CD Sep 95 RL.toast / mac / Technical Documentation / develop / develop Issue 16 code / CollaboDraw / windutils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-24  |  10.3 KB  |  420 lines  |  [TEXT/MPS ]

  1. /*-------------------------------------------------------------------------------------
  2.  *
  3.  * Simple Sample PowerTalk Application Framework
  4.  *
  5.  * ©1991-1993 Apple Computer
  6.  *
  7.  -------------------------------------------------------------------------------------*/
  8. /*
  9.  * windutils.c -- window drawing/scrolling utility functions
  10.  *
  11.  * change history:
  12.  *
  13.  * SJF        08/23/93        1.0f1        update to final headers, fix comments
  14.  * SJF        04/21/93        1.0b2        update to b2
  15.  * SJF        03/01/93        1.0b1        added digital signatures
  16.  * SJF        02/09/93        1.0b1        update to b1
  17.  * SJF        10/13/92        1.0d4        update to a11
  18.  * SJF        09/09/92        1.0d3        update to a9
  19.  * SJF        05/07/92        1.0d2        update to a6
  20.  * SJF        11/06/91        1.0d1        initial coding
  21.  *
  22.  */
  23.  
  24. #pragma segment othersegment
  25.  
  26. #ifndef __TYPES__
  27. #include <Types.h>
  28. #endif
  29.  
  30. #ifndef __QUICKDRAW__
  31. #include <QuickDraw.h>
  32. #endif
  33.  
  34. #ifndef __WINDOWS__
  35. #include <Windows.h>
  36. #endif
  37.  
  38. #ifndef __MEMORY__
  39. #include <Memory.h>
  40. #endif
  41.  
  42. #ifndef __MENUS__
  43. #include <Menus.h>
  44. #endif
  45.  
  46. #ifndef __OCESTANDARDMAIL__
  47. #include <OCEStandardMail.h>
  48. #endif
  49.  
  50. #include "const.h"
  51. #include "strconst.h"
  52. #include "mytypes.h"
  53. #include "globals.h"
  54. #include "utils.h"
  55. #include "windowstuff.h"
  56. #include "mystandardmail.h"
  57. #include "draw.window.h"
  58.  
  59. #include "windutils.h"
  60.  
  61.  
  62. /* clips out all of the window except the grow icon location to draw the grow icon */
  63. /* this is to avoid the problem caused when you have a window crossing a B&W and a color screen */
  64.  
  65. void MyDrawGrowIcon(WindowPtr window)
  66. {
  67.     GrafPtr savePort;
  68.     RgnHandle saveClip;
  69.     Rect gbRect;
  70.     
  71.     gbRect = window->portRect;
  72.     gbRect.top = gbRect.bottom - kGrowBoxWidth;
  73.     gbRect.left = gbRect.right - kGrowBoxWidth;
  74.     GetPort(&savePort);
  75.     SetPort(window);
  76.     
  77.     saveClip = NewRgn();
  78.     GetClip(saveClip);
  79.     ClipRect(&gbRect);
  80.     
  81.     DrawGrowIcon(window);
  82.     
  83.     SetClip(saveClip);
  84.     DisposeRgn(saveClip);
  85.     
  86.     SetPort(savePort);
  87. }
  88.  
  89.  
  90. /* clip the window to just the drawing area */
  91.  
  92. void ClipToDrawing(WindowPtr window,WInfoPtr infoPtr)
  93. {
  94.     Rect newClipRect;
  95.     
  96.     newClipRect = window->portRect;
  97.     newClipRect.top += infoPtr->topIndent;
  98.     newClipRect.left += infoPtr->leftIndent;
  99.     ClipRect(&newClipRect);
  100. }
  101.  
  102.  
  103. /* get the region containing the scroll bars */
  104.  
  105. RgnHandle GetScrollersRgn(Rect *windowRect)
  106. {
  107.     RgnHandle windowRgn;
  108.     Rect wRect;
  109.     
  110.     windowRgn = NewRgn();
  111.  
  112.     OpenRgn();
  113.     SetRect(&wRect,windowRect->left,windowRect->bottom-kScrollBarWidth,
  114.                 windowRect->right-kScrollBarWidth,windowRect->bottom);
  115.     FrameRect(&wRect);
  116.     SetRect(&wRect,windowRect->right-kScrollBarWidth,windowRect->top,
  117.                 windowRect->right,windowRect->bottom);
  118.     FrameRect(&wRect);
  119.     CloseRgn(windowRgn);
  120.     
  121.     return windowRgn;
  122. }
  123.  
  124.  
  125. /* make our scroll bars */
  126.  
  127. void MakeScrollBars(WindowPtr window,WInfoPtr infoPtr)
  128. {
  129.     GrafPtr savePort;
  130.     Rect cntrlRect;
  131.     
  132.     infoPtr->otherData[kViewOffset] = 0;
  133.     
  134.     GetPort(&savePort);
  135.     SetPort(window);
  136.  
  137.     cntrlRect.left += infoPtr->leftIndent;
  138.     cntrlRect = window->portRect;
  139.     InsetRect(&cntrlRect,-1,-1);
  140.     cntrlRect.top = cntrlRect.bottom - kScrollBarWidth;
  141.     cntrlRect.right = cntrlRect.right - kGrowBoxWidth;
  142.     HSCROLL = NewControl(window,&cntrlRect,"\p",false,0,0,0,scrollBarProc,0);
  143.     ValidRect(&cntrlRect);
  144.  
  145.     cntrlRect = window->portRect;
  146.     InsetRect(&cntrlRect,-1,-1);
  147.     cntrlRect.left = cntrlRect.right - kScrollBarWidth;
  148.     cntrlRect.bottom = cntrlRect.bottom - kGrowBoxWidth;
  149.     cntrlRect.top += infoPtr->topIndent;
  150.     VSCROLL = NewControl(window,&cntrlRect,"\p",false,0,0,0,scrollBarProc,0);
  151.     ValidRect(&cntrlRect);
  152.  
  153.     ReCalcScrollBars(window,infoPtr);
  154.     ShowControl((ControlHandle)HSCROLL);
  155.     ShowControl((ControlHandle)VSCROLL);
  156.     
  157.     SetPort(savePort);
  158. }
  159.  
  160.  
  161. /* move the scroll bars when the window moves */
  162.  
  163. void MoveScrollBars(WindowPtr window)
  164. {
  165.     WInfoPtr infoPtr;
  166.     char hState;
  167.     Rect cntrlRect;
  168.     GrafPtr savePort;
  169.     
  170.     GetPort(&savePort);
  171.     SetPort(window);
  172.     
  173.     infoPtr = BeginWindowAccess(window,&hState);
  174.  
  175.     HideControl((ControlHandle)VSCROLL);
  176.     HideControl((ControlHandle)HSCROLL);
  177.  
  178.     if (infoPtr->topIndent<=(window->portRect.bottom-kGrowBoxWidth) &&
  179.         infoPtr->leftIndent<=(window->portRect.right-kGrowBoxWidth)) {
  180.         
  181.         cntrlRect = window->portRect;
  182.         InsetRect(&cntrlRect,-1,-1);
  183.         cntrlRect.top = cntrlRect.bottom - kScrollBarWidth;
  184.         cntrlRect.right = cntrlRect.right - kGrowBoxWidth;
  185.         cntrlRect.left += infoPtr->leftIndent;
  186.         MoveControl(HSCROLL,cntrlRect.left,cntrlRect.top);
  187.         SizeControl(HSCROLL,cntrlRect.right-cntrlRect.left,cntrlRect.bottom-cntrlRect.top);
  188.         ValidRect(&cntrlRect);
  189.     
  190.         cntrlRect = window->portRect;
  191.         InsetRect(&cntrlRect,-1,-1);
  192.         cntrlRect.left = cntrlRect.right - kScrollBarWidth;
  193.         cntrlRect.bottom = cntrlRect.bottom - kGrowBoxWidth;
  194.         cntrlRect.top += infoPtr->topIndent;
  195.         MoveControl(VSCROLL,cntrlRect.left,cntrlRect.top);
  196.         SizeControl(VSCROLL,cntrlRect.right-cntrlRect.left,cntrlRect.bottom-cntrlRect.top);
  197.         ValidRect(&cntrlRect);
  198.     
  199.         ReCalcScrollBars(window,infoPtr);
  200.         
  201.         ShowControl(HSCROLL);
  202.         ShowControl(VSCROLL);
  203.  
  204.     }
  205.     
  206.     EndWindowAccess(window,hState);
  207.     SetPort(savePort);
  208. }
  209.  
  210.  
  211. /* recalculate the scroll bars when the window changes */
  212.  
  213. void ReCalcScrollBars(WindowPtr window,WInfoPtr infoPtr)
  214. {
  215.     Rect viewBounds,pageRect;
  216.     short vMax,hMax;
  217.     Point *scrollPos;
  218.     
  219.     GetViewBounds(window,infoPtr,&viewBounds);
  220.     pageRect = (**(infoPtr->printRecord)).prInfo.rPage;
  221.     scrollPos = (Point *)&infoPtr->otherData[kViewOffset];
  222.     
  223.     hMax = (pageRect.right-pageRect.left) - (viewBounds.right-viewBounds.left);
  224.     vMax = (pageRect.bottom-pageRect.top) - (viewBounds.bottom-viewBounds.top);
  225.     
  226.     if (hMax<0)
  227.         hMax=0;
  228.     if (vMax<0)
  229.         vMax=0;
  230.     
  231.     if ( (scrollPos->h+(viewBounds.right-viewBounds.left)) > (pageRect.right-pageRect.left) )
  232.         scrollPos->h = (pageRect.right-pageRect.left) - (viewBounds.right-viewBounds.left);
  233.     if ( (scrollPos->v+(viewBounds.bottom-viewBounds.top)) > (pageRect.bottom-pageRect.top) )
  234.         scrollPos->v = (pageRect.bottom-pageRect.top) - (viewBounds.bottom-viewBounds.top);
  235.         
  236.     SetCtlMax((ControlHandle)HSCROLL,hMax);
  237.     SetCtlMax((ControlHandle)VSCROLL,vMax);
  238.     SetCtlValue((ControlHandle)HSCROLL,scrollPos->h);
  239.     SetCtlValue((ControlHandle)VSCROLL,scrollPos->v);
  240.     SetPort(window);
  241. }
  242.  
  243.  
  244. /* action callback to scroll the document */
  245.  
  246. pascal void ScrollActionProc(ControlHandle theControl,short part)
  247. {
  248.     short ctlValue,delta,ctlMin,ctlMax,pageDistance,deltaH,deltaV;    
  249.     Rect viewRect;
  250.     WindowPtr window;
  251.     WInfoPtr infoPtr;
  252.     char hState;
  253.     
  254.     if (part==0)
  255.         return;
  256.         
  257.     window = (**theControl).contrlOwner;
  258.     if (!IsAppWindow(window))
  259.         return;
  260.     
  261.     infoPtr = BeginWindowAccess(window,&hState);
  262.     
  263.     GetViewBounds(window,infoPtr,&viewRect);
  264.     pageDistance = viewRect.bottom-viewRect.top;
  265.     
  266.     ctlValue = GetCtlValue(theControl);
  267.     ctlMin = GetCtlMin(theControl);
  268.     ctlMax = GetCtlMax(theControl);
  269.     
  270.     switch (part) {
  271.         case inUpButton:
  272.             delta = -kScrollDistance;
  273.             break;
  274.         case inDownButton:
  275.             delta = kScrollDistance;
  276.             break;
  277.         case inPageUp:
  278.             delta = -pageDistance;
  279.             break;
  280.         case inPageDown:
  281.             delta = pageDistance;
  282.             break;
  283.     }
  284.         
  285.     if ( (ctlValue+delta)>ctlMax )
  286.         delta = ctlMax-ctlValue;
  287.     else if ( (ctlValue+delta)<ctlMin )
  288.         delta = ctlMin-ctlValue;
  289.         
  290.     SetCtlValue(theControl,ctlValue+delta);
  291.     
  292.     deltaH = deltaV = 0;
  293.     if (theControl==(ControlHandle)HSCROLL)
  294.         deltaH = delta;
  295.     else if (theControl==(ControlHandle)VSCROLL)
  296.         deltaV = delta;
  297.     
  298.     MoveGraphics(window,infoPtr,deltaH,deltaV);
  299.  
  300.     EndWindowAccess(window,hState);
  301. }
  302.  
  303.  
  304. /* move the shapes in the window */
  305.  
  306. void MoveGraphics(WindowPtr window,WInfoPtr infoPtr,short deltaH,short deltaV)
  307. {
  308.     GrafPtr savePort;
  309.     Rect drawingArea;
  310.     RgnHandle updateRgn,saveClip;
  311.     Point *scrollPos,drawOffset;
  312.     
  313.     GetDrawingAreaBounds(window,infoPtr,&drawingArea);
  314.     
  315.     // store new scroll position into window data
  316.     
  317.     scrollPos = (Point *)&infoPtr->otherData[kViewOffset];
  318.     scrollPos->h += deltaH;
  319.     scrollPos->v += deltaV;
  320.  
  321.     drawOffset = *scrollPos;
  322.     drawOffset.h = infoPtr->leftIndent-drawOffset.h;
  323.     drawOffset.v = infoPtr->topIndent-drawOffset.v;
  324.     
  325.     // do drawing of scrolling
  326.     
  327.     GetPort(&savePort);
  328.     SetPort(window);
  329.     updateRgn = NewRgn();
  330.     saveClip = NewRgn();
  331.     ScrollRect(&drawingArea,-deltaH,-deltaV,updateRgn);
  332.     GetClip(saveClip);
  333.     SetClip(updateRgn);
  334.     DrawAllShapes(infoPtr,drawOffset);
  335.     SetClip(saveClip);
  336.     DisposeRgn(updateRgn);
  337.     DisposeRgn(saveClip);
  338.     SetPort(savePort);
  339. }
  340.  
  341.  
  342. /* check to make sure we're not bigger than our print area */
  343.  
  344. Boolean CheckPageSize(WindowPtr window,WInfoPtr infoPtr)
  345. {
  346.     Rect oldSize;
  347.     short newWidth,newHeight,oldWidth,oldHeight;
  348.     GrafPtr savePort;
  349.     Boolean changed;
  350.     
  351.     ClipPageSize(&window->portRect,infoPtr,&newWidth,&newHeight);
  352.     oldWidth = window->portRect.right-window->portRect.left;
  353.     oldHeight = window->portRect.bottom-window->portRect.top;
  354.     
  355.     // force a new window size on the user since they changed page setup on us
  356.     
  357.     if (newHeight!=oldHeight || newWidth!=oldWidth) {
  358.         GetPort(&savePort);
  359.         SetPort(window);
  360.         oldSize = window->portRect;
  361.         SizeWindow(window,newWidth,newHeight,true);
  362.         InvalRect(&window->portRect);
  363.         SendWindowMessage(window,kResizeMessage,&oldSize);
  364.         SetPort(savePort);
  365.         changed = true;
  366.     }    
  367.     else
  368.         changed = false;
  369.     
  370.     return changed;
  371. }
  372.  
  373.  
  374. /* make our drawing only as big as our print area */
  375.  
  376. void ClipPageSize(Rect *wRect,WInfoPtr infoPtr,short *maxWidth,short *maxHeight)
  377. {
  378.     Rect printRect,drawAreaRect;
  379.     
  380.     printRect = (**(infoPtr->printRecord)).prInfo.rPage;
  381.  
  382.     drawAreaRect.top = infoPtr->topIndent;
  383.     drawAreaRect.left = infoPtr->leftIndent;
  384.     drawAreaRect.bottom = wRect->bottom-wRect->top-kScrollBarWidth;
  385.     drawAreaRect.right = wRect->right-wRect->left-kScrollBarWidth;
  386.  
  387.     *maxWidth = wRect->right-wRect->left;
  388.     *maxHeight = wRect->bottom-wRect->top;
  389.     if ( (drawAreaRect.right-drawAreaRect.left) > (printRect.right-printRect.left) )
  390.         *maxWidth = (printRect.right-printRect.left)+infoPtr->leftIndent+kScrollBarWidth;
  391.     if ( (drawAreaRect.bottom-drawAreaRect.top) > (printRect.bottom-printRect.top) )
  392.         *maxHeight = (printRect.bottom-printRect.top)+infoPtr->topIndent+kScrollBarWidth;
  393. }
  394.  
  395.  
  396. /* get the bounds of the view that we can see */
  397.  
  398. void GetViewBounds(WindowPtr window,WInfoPtr infoPtr,Rect *bounds)
  399. {
  400.     Point viewOffset;
  401.     
  402.     viewOffset = *(Point *)&infoPtr->otherData[kViewOffset];
  403.     GetDrawingAreaBounds(window,infoPtr,bounds);
  404.     OffsetRect(bounds,-infoPtr->topIndent,-infoPtr->leftIndent);
  405.     OffsetRect(bounds,viewOffset.h,viewOffset.v);
  406. }
  407.  
  408.  
  409. /* get the bounds of the drawing that we can see */
  410.  
  411. void GetDrawingAreaBounds(WindowPtr window,WInfoPtr infoPtr,Rect *bounds)
  412. {
  413.     bounds->top = infoPtr->topIndent;
  414.     bounds->left = infoPtr->leftIndent;
  415.     bounds->bottom = window->portRect.bottom-window->portRect.top-kScrollBarWidth;
  416.     bounds->right = window->portRect.right-window->portRect.left-kScrollBarWidth;
  417. }
  418.  
  419.  
  420.